
<template>
    <div class="set-scan-cats">
        <div class="cats">
            <nav-tree :value="s_id"
                      :model="cats"
                      class-name=""
                      children="sub_cats"
                      label-name="name"
                      :value-name="'id'"
                      is-expanded="true"
                      @selectID="select_id"
            ></nav-tree>
        </div>
        <div>
            <!--分类名称:<input type="text" placeholder="" class="input-one" v-model="s_cat.name">-->
            <!--CID：<input type="text" placeholder="" class="input-one" v-model="s_cat.cid">-->
            <!--属性：<input type="text" placeholder="" class="input-one" v-model="s_cat.props">-->
            <!--SKU属性：<input type="text" placeholder="" class="input-one" v-model="s_cat.sku_props">-->
            <!--类型：<input type="text" placeholder="" class="input-one" v-model="s_cat.type">-->
            <!--隐藏：<input type="checkbox" placeholder="" class="input-one" v-model="s_cat.hidden" true-value="1" false-value="0">-->
            <!--<div @click="save()">保存</div>-->

            <ul>
                    <li>分类名称:<input type="text" placeholder="" class="input-one" v-model="s_cat.name"></li>
                    <li>CID：<input type="text" placeholder="" class="input-one" v-model="s_cat.cid"></li>
                    <li>属性：<input type="text" placeholder="" class="input-one" v-model="s_cat.props"></li>
                    <li>SKU属性：<input type="text" placeholder="" class="input-one" v-model="s_cat.sku_props"></li>
                    <li>类型：<input type="text" placeholder="" class="input-one" v-model="s_cat.type"></li>
                    <li>catId:<input type="text" disabled v-model="s_cat.id" ></li>
                    <li>隐藏：<input type="checkbox" placeholder="" class="input-one" v-model="s_cat.hidden" true-value="1" false-value="0"></li>
                    <div @click="save()">保存</div>
            </ul>
        </div>
    </div>
</template>

<script>
    import navTree from 'admin/js/common/nav-tree.vue';

    export default {
        props: [''],
        data() {
            return {
                cats: [],
                s_id: 0,
                s_cat: {}
            }
        },
        methods: {
            select_id(s_id,item){
                this.s_id = s_id;
                this.s_cat = item;
            },
            save(){
                var self = this;


                $.ajax({
                    type:'POST',
                    url: CFG.api.mac.saveCat,
                    dataType:'json',
                    data: self.s_cat,
                    error:function(error){
                        //alert(JSON.stringify(error));
                    },
                    success:function(response){
                        if(response.err){
                            //alert(response.err.msg);
                            //response.err.sub_msg = decodeURIComponent(response.err.sub_msg);
                            //fn.tx(response);
                        }else{

                        }
                    }
                });
            }
        },
        created(){
            var self = this;

            $.ajax({
                type:'POST',
                url: CFG.api.mac.getCats,
                dataType:'json',
                data:{
                    //parentID: 0
                },
                error:function(error){
                    //alert(JSON.stringify(error));
                },
                success:function(response){
                    if(response.err){
                        //alert(response.err.msg);
                        //response.err.sub_msg = decodeURIComponent(response.err.sub_msg);
                        //fn.tx(response);
                    }else{
                        /*var mac_cats = [];
                        $.each(response.data,function (i,t) {
                            mac_cats.push({
                                parent_id: t.parent_id,
                                id: t.id,
                                name: t.name
                            });
                        });

                        var all_cats = {};
                        $.each (mac_cats,function(i,t){
                            var tmp_sub_cats = {};

                            var cat_name = t['name'];
                            cat_name = cat_name.replace(/(^\s*)/g, "");

                            all_cats[t['id']] = {
                                parent_id: t['parent_id'],
                                id: _.toString(t['id']),
                                name: cat_name,
                                sub_cats: tmp_sub_cats,
                                children_id: 0
                            };
                            if(t['parent_id'] != 0){
                                all_cats[t['parent_id']]['children_id'] = t['parent_id'];
                            }

                        });


                        function make(cats){
                            //var index = 0;
                            var edit = [];
                            var del = [];
                            $.each (cats , function(i,t){
                                if(t['children_id'] == 0 && t['parent_id'] != 0){
                                    cats[t['parent_id']]['sub_cats'][t['id']] = t;
                                    edit.push(t['parent_id']);
                                    del.push(t['id']);
                                }
                                //index++;
                            });

                            edit = _.uniq(edit);

                            $.each (del, function(i,t) {
                                //var index_i = 0;
                                $.each (cats,function(ii,tt) {
                                    if(tt['id'] == t){
                                        delete(cats[tt['id']]);
                                    }
                                    //index_i++;
                                })
                            });


                            $.each (edit,function(i,t) {
                                $.each (cats,function(ii,tt) {
                                    if(tt['id'] == t){
                                        //dump($cats[$cat['id']]);
                                        cats[tt['id']]['children_id'] = 0;

                                        cats[tt['id']]['sub_cats'] = _.values(cats[tt['id']]['sub_cats']);
                                    }
                                })
                            });


                            if(edit.length < 1) {
                                return cats;
                            }else{
                                return make(cats);
                                //return $cats;
                            }
                        }

                        all_cats = _.values(make(all_cats));


                        self.cats = [{
                            id: 0,
                            name: '所有图片',
                            sub_cats: all_cats
                        }];*/

                        self.cats = response.data;
                    }
                }
            });

        },
        components: {
            navTree
        }
    }
</script>

<style lang="less">
    .set-scan-cats{
        display: flex;
    }
</style>